#include "reg51.h"

#define ADDR0 P1_0
#define ADDR1 P1_1
#define ADDR2 P1_2
#define ADDR3 P1_3
#define ENLED P1_4

#define KeyIn4 P2_7

// 数码管显示字符转换表
unsigned char __code LedChar[] = {
        0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
        0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

int main() {
    unsigned char cnt = 0;
    __bit backup = 1;

    ENLED = 0;
    ADDR3 = 1;
    ADDR2 = 0;
    ADDR1 = 0;
    ADDR0 = 0;

    P2 = 0xF7;  // KeyOut1 拉低

    P0 = LedChar[cnt];

    while (1) {
        if (KeyIn4 != backup) {
            if (backup == 1) {  // 判断按下
                cnt++;
                cnt = cnt > 9 ? 0 : cnt;
                P0 = LedChar[cnt];
            }
            backup = KeyIn4;
        }
    }

}
